#include <gtest/gtest.h>
#include "../mqserver/binding.hpp"

using namespace std;
using namespace ns_rabbitmq;

BindManager::BindingManagePtr bptr;
class BindingTets : public testing::Environment
{
    virtual void SetUp() override
    {
        bptr = std::make_shared<BindManager>("./data/meta.db");
    }
    virtual void TearDown()
    {
        // bptr->Clear();
        std::cout << "完成清理工作" << std::endl;
    }
};

// TEST(binding_test, insert_test)
// {
//     bptr->Bind("exchange1", "queue1", "news.music.#", true);
//     bptr->Bind("exchange1", "queue2", "news.sport.#", true);
//     bptr->Bind("exchange1", "queue3", "news.gossip.#", true);
//     bptr->Bind("exchange2", "queue1", "news.music.pop", true);
//     bptr->Bind("exchange2", "queue2", "news.sport.#", true);
//     bptr->Bind("exchange2", "queue3", "news.gossip.#", true);
//     ASSERT_EQ(bptr->Size(), 6);
// }

TEST(binding_test, select_test)
{
    ASSERT_EQ(bptr->IsExistsBinding("exchange1", "queue1"), true);
    ASSERT_EQ(bptr->IsExistsBinding("exchange1", "queue2"), true);
    ASSERT_EQ(bptr->IsExistsBinding("exchange1", "queue3"), true);
    ASSERT_EQ(bptr->IsExistsBinding("exchange2", "queue1"), true);
    ASSERT_EQ(bptr->IsExistsBinding("exchange2", "queue2"), true);
    ASSERT_EQ(bptr->IsExistsBinding("exchange2", "queue3"), true);

    BindingPtr ptr = bptr->Getbinding("exchange1", "queue1");
    ASSERT_NE(ptr.get(), nullptr);
    ASSERT_EQ(ptr->_queue_name, string("queue1"));
    ASSERT_EQ(ptr->_exchange_name, string("exchange1"));
    ASSERT_EQ(ptr->_binding_key, string("news.music.#"));
}

// e1--q1
// e1--q2
// e1--q3
// e2--q1
// e2--q2
// e2--q3
TEST(binding_test, select_exchange_test)
{
    QueueMap qmp = bptr->GetExchangeBindings("exchange1");
    ASSERT_EQ(qmp.size(), 3);
    ASSERT_NE(qmp.find("queue1"), qmp.end());
    ASSERT_NE(qmp.find("queue2"), qmp.end());
    ASSERT_NE(qmp.find("queue3"), qmp.end());
}

// e1
// e1--q2
// e1--q3
// e2
// e2--q2
// e2--q3
TEST(binding_test, remove_queue_test)
{
    bptr->RemoveQueueBinding("queue1");
    ASSERT_EQ(bptr->Size(), 4);
    ASSERT_EQ(bptr->IsExistsBinding("exchange1", "queue1"), false);
    ASSERT_EQ(bptr->IsExistsBinding("exchange2", "queue1"), false);
}

// e2
// e2--q2
// e2--q3

TEST(binding_test, remove_exchange_test)
{
    bptr->RemoveExchangeBindings("exchange1");
    ASSERT_EQ(bptr->IsExistsBinding("exchange1", "queue2"), false);
    ASSERT_EQ(bptr->IsExistsBinding("exchange2", "queue2"), true);
}

TEST(queue_test, remove_single_test)
{
    bptr->UnBind("exchange2", "queue2");
    bptr->UnBind("exchange2", "queue3");
    ASSERT_EQ(bptr->IsExistsBinding("exchange2", "queue2"), false);
    ASSERT_EQ(bptr->IsExistsBinding("exchange2", "queue3"), false);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new BindingTets());
    RUN_ALL_TESTS();
    return 0;
}